import enum

import pygame

from Dialog.Dialog_For_Actor import Task3_CaptianDialog, Task3_Vice_CaptianDialog
from Dialog.Dialog_For_Systerm import TunnelTask3BeginDialog, TunnelTask3EndDialog, TunnelTaskFailDialog
from Task.BaseTask import BaseTask, TaskStatus


class FightTaskStatus(enum.IntEnum):
    finding_captain = 1  # 找队长
    found_captain = 2  # 找到队长，对话中
    finding_vice_captain = 3  # 找副队长
    found_vice_captain = 4  # 副队长，对话中
    finding_prize = 5  # 找东西
    found_prize = 6  # 找完东西
    ok = 7  # 带信成功
    fail = 8  # 带信失败


class FightTask(BaseTask):
    def __init__(self, lead_actor, prizes_group, captain, vice_captain, font):

        self.score = 0
        self.lead_actor = lead_actor
        self.prizes_group = prizes_group
        self.captain = captain
        self.vice_captain = vice_captain
        self.state = FightTaskStatus.finding_captain
        self.captain_dialog = Task3_CaptianDialog(self.captain, font)
        self.vice_captain_dialog = Task3_Vice_CaptianDialog(self.vice_captain, font)
        self.task_dialog = TunnelTask3BeginDialog(font)
        self.win_dialog = TunnelTask3EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        self.time_count = 2000
        super(FightTask, self).__init__(self.task_dialog, self.win_dialog, self.fail_dialog)

    def __do_collide_prize(self):
        """
        物品碰撞检测
        :return:
        """
        # 碰撞范围大一点
        self.lead_actor.rect = pygame.Rect(self.lead_actor.pos_x + 5, self.lead_actor.pos_y + 5, 35, 60)
        # 碰撞检测
        collide_list = pygame.sprite.spritecollide(self.lead_actor, self.prizes_group, True)
        # 再把范围改回来
        self.lead_actor.rect = pygame.Rect(self.lead_actor.pos_x + 5, self.lead_actor.pos_y + 5, 35, 60)
        if len(collide_list) > 0:
            self.score += 1
            self.sound.play()
        if self.score == 5:
            return True

    def __do_collide_actor(self, actor):
        """
        人物碰撞检测
        :param actor:
        :return:
        """
        temp_rect = self.lead_actor.rect
        # 调大碰撞范围
        self.lead_actor.rect = pygame.Rect(self.lead_actor.pos_x + 5,
                                           self.lead_actor.pos_y + 5, 35, 70)
        is_collide = pygame.sprite.collide_rect(self.lead_actor, actor)
        self.lead_actor.rect = temp_rect
        if is_collide:
            return True

    def draw_task(self, current_surface, x, y):
        """
        绘制人物和对话框
        :param current_surface: 
        :param x: 
        :param y: 
        :return: 
        """
        self.captain.draw(current_surface, x, y)
        if self.state == FightTaskStatus.found_captain:
            self.captain_dialog.draw(current_surface, x, y)
            if self.captain_dialog.is_finish():
                self.state = FightTaskStatus.finding_prize

        elif self.state == FightTaskStatus.found_vice_captain:
            self.vice_captain_dialog.draw(current_surface, x, y)
            if self.vice_captain_dialog.is_finish():
                self.state = FightTaskStatus.ok
                self.task_status = TaskStatus.win_dialog

        if self.state >= FightTaskStatus.finding_vice_captain:
            self.vice_captain.draw(current_surface, x, y)

    def do_self_task(self, key_down, key_list):
        """
        任务的执行
        :param key_down:
        :param key_list:
        :return:
        """
        # 找队长
        if self.state == FightTaskStatus.finding_captain:
            # 找到队长
            if self.__do_collide_actor(self.captain):
                self.state = FightTaskStatus.found_captain

        # 找东西
        elif self.state == FightTaskStatus.finding_prize:
            # 找到东西
            if self.__do_collide_prize():
                self.state = FightTaskStatus.finding_vice_captain

        # 找副队长
        elif self.state == FightTaskStatus.finding_vice_captain:
            # 找到副队长
            if self.__do_collide_actor(self.vice_captain):
                self.state = FightTaskStatus.found_vice_captain
        # 找到队长后按键处理字幕
        elif self.state == FightTaskStatus.found_captain:
            if key_down and key_list[pygame.K_SPACE]:
                self.captain_dialog.next_text()
            if self.captain_dialog.is_finish():
                self.state = FightTaskStatus.finding_prize
        # 找到副队长后按键处理字幕
        elif self.state == FightTaskStatus.found_vice_captain:
            if key_down and key_list[pygame.K_SPACE]:
                self.vice_captain_dialog.next_text()
            if self.vice_captain_dialog.is_finish():
                self.state = FightTaskStatus.ok
                self.task_status = TaskStatus.win_dialog
        # 计时
        self.time_count -= 1
        if self.time_count <= 0:
            self.task_status = TaskStatus.fail_dialog
